package com.kayo.specimen.realm;

import com.kayo.specimen.model.Privilege;
import com.kayo.specimen.model.Role;
import com.kayo.specimen.model.User;
import com.kayo.specimen.model.view.UserView;
import com.kayo.specimen.service.RoleService;
import com.kayo.specimen.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;

public class CustomRealm extends AuthorizingRealm {
    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    /**
     * @MethodName doGetAuthorizationInfo
     * @Description 权限配置类
     * @Param [principalCollection]
     * @Return AuthorizationInfo
     * @Author WangShiLin
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取登录用户名
        String code = (String) principalCollection.getPrimaryPrincipal();
        //查询用户名称
        List<UserView> users = userService.getUsers(code, null, null, null);
        if (users == null || users.isEmpty()) {
            throw new UnknownAccountException("");
        }

        //添加角色和权限
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        for (Role role : userService.getGrantedRoles(users.get(0).getId())) {
            //添加角色
            simpleAuthorizationInfo.addRole(role.getName());
            //添加权限
            for (Privilege privilege : roleService.getGrantedPrivileges(role.getId())) {
                simpleAuthorizationInfo.addStringPermission(privilege.getName());
            }
        }
        return simpleAuthorizationInfo;
    }

    /**
     * @MethodName doGetAuthenticationInfo
     * @Description 认证配置类
     * @Param [authenticationToken]
     * @Return AuthenticationInfo
     * @Author WangShiLin
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        if (StringUtils.isEmpty(authenticationToken.getPrincipal())) {
            throw new UnknownAccountException("");
        }
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) authenticationToken;
        List<UserView> users = userService.getUsers(usernamePasswordToken.getUsername(), null, null,null);
        if (users == null || users.isEmpty()) {
            //这里返回后会报出对应异常
            throw new UnknownAccountException("帐户不存在");
        } else {
            //验证密码
            User user = users.get(0);
            if(!user.getEnabled())
                throw new ExpiredCredentialsException("用户已经失效");

            String psw1 = new String(usernamePasswordToken.getPassword());
            String psw2 = user.getPassword();
            if (!psw1.equals(psw2)) {
                throw new AuthenticationException("密码验证失败");
            }
            //这里验证authenticationToken和simpleAuthenticationInfo的信息
            return new SimpleAuthenticationInfo(usernamePasswordToken.getUsername(), usernamePasswordToken.getPassword(), getName());
        }
    }
}
